/*
package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.entity.Dept;
import com.aaa.service.DeptService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

*/
/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/12/7
 *//*

@Service
public class DeptServiceImpl2 implements DeptService {

    @Resource
    private DeptDao deptDao;


    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    */
/*根据id查询*//*

    public Dept findById(Integer id) {
        //1.查询缓存:key的名称：【模块名称::类名::唯一主键】
        ValueOperations<String, Object> forValue = redisTemplate.opsForValue();
        Object o = forValue.get("dept::" + id);
        if(o!=null){
            return (Dept) o;
        }
        Dept dept = deptDao.selectById(id);
        if(dept!=null){
            //放入查询到的数据到缓存,如果不加过期时间，缓存中的数据会越来越多影响内存
            forValue.set("dept::"+id,dept,3600, TimeUnit.SECONDS);
        }
        return dept;
    }

    @Override
    public Integer delete(Integer id) {
        //只删除了数据库但缓存没有删除（先删缓存）
        Boolean delete = redisTemplate.delete("dept::" + id);
        return null;
    }

    @Override
    public Dept save(Dept dept) {
        //没必要考虑缓存
        deptDao.insert(dept);
        return dept;
    }

    @Override
    public Dept update(Dept dept) {
        //只修改了数据库，但是缓存中的数据还是之前的(缓存还未同步修改)
        deptDao.updateById(dept);
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        opsForValue.set("dept::"+dept.getDeptno(),dept,3600,TimeUnit.SECONDS);

        return dept;
    }
}
*/
